package utils.db.impl;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pojo.EDeviceState;
import pojo.EDeviceType;
import utils.db.api.IDeviceAdd;

public class DeviceAdd extends DBOperation implements IDeviceAdd {
	private static Logger logger = LoggerFactory.getLogger(DeviceAdd.class);

	public DeviceAdd() {
		super();
	}

	@Override
	public void addDevice(String desc, EDeviceType type, int clientId, int servicemanId) {
		initializeQuery(desc, type,clientId,servicemanId);
		logger.info("Added new device " + desc + " " + type.name() + " "
				+ EDeviceState.WAITING.name() + " " + getCurrentDate());
	}


	private void initializeQuery(String desc, EDeviceType type, int clientId, int servicemanId) {
		try {
			pstmt = super.getConnector().prepareStatement(ADD_DEVICE);
			pstmt.setInt(1, clientId);
			pstmt.setInt(2, servicemanId);
			pstmt.setString(3, desc);
			pstmt.setString(4, type.name());
			pstmt.setString(5, EDeviceState.WAITING.name());
			pstmt.setDate(6, getCurrentDate());
			pstmt.execute();

		} catch (SQLException e) {

			logger.error("Error during add new device");
			e.printStackTrace();
		}
	}

	public static Date getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		java.util.Date utilDate = cal.getTime();
		java.sql.Date sqlDate = new Date(utilDate.getTime());
		return sqlDate;
	}



}
